#include "pe_ccnode.h"
#include "CCQTSceneDirector.h"
#include "qtcoco\CocoHieriachy.h"
#include "qtcore"


PE_CCNode::PE_CCNode(QtVariantPropertyManager *variantManager):PEditor(variantManager)
{
    
}


QtProperty * PE_CCNode::initUI()
{
    addProperty(QVariant::PointF, "Position");
    addProperty(QVariant::PointF, "AnchorPoint");
    addProperty(QVariant::SizeF, "ContentSize");
    QtVariantProperty * item = addProperty(QVariant::PointF, "ScaleXY");
    item = addProperty(QVariant::Double, "Rotation");
	addProperty(QVariant::Int, "Z-order");
	addProperty(QVariant::Int,  "Tag");
// 	item->setAttribute("singleStep" ,0.1f);
// 	item->setAttribute("minimum" , 0);

    return m_topItem;
}

void PE_CCNode::refreshProperty(QString propertyName)
{
    CCNode * cnode = CCQTSceneDirector::sharedSceneDirector()->CurrentCNode();
	if (cnode == NULL) return;
    Property(propertyName)->setValue(QPointF(cnode->getPositionX() , cnode->getPositionY()) );
}


void PE_CCNode::refresh()
{
	CCNode * cnode = CCQTSceneDirector::sharedSceneDirector()->CurrentCNode();
	if (cnode == NULL) return;
	Property("Position")->setValue(QPointF(cnode->getPositionX() , cnode->getPositionY()) );
	Property("AnchorPoint")->setValue(QPointF(cnode->getAnchorPoint().x, cnode->getAnchorPoint().y));
	Property("ContentSize")->setValue(QSizeF(cnode->getContentSize().width , cnode->getContentSize().height));
	Property("ScaleXY")->setValue(QPointF(cnode->getScaleX(),cnode->getScaleY()));
    Property("Rotation")->setValue(cnode->getRotation());
	Property("Z-order")->setValue(cnode->getZOrder());
	Property("Tag")->setValue(cnode->getTag());
}

void PE_CCNode::valueChanged(QtProperty * property, QVariant variant)
{
	CCNode * cnode = CCQTSceneDirector::sharedSceneDirector()->CurrentCNode();
	if (cnode == NULL) return;
    
	if (property == Property("Position")) 
	{
		QPointF point =variant.value<QPointF>();
		cnode->setPosition(point.x(),point.y());
	}
	else if ( property == Property("AnchorPoint") )
	{
		QPointF point =variant.value<QPointF>();
		cnode->setAnchorPoint(ccp(point.x(),point.y()));
	}
	else if ( property == Property("ContentSize") )
	{
		QSizeF size =variant.value<QSizeF>();
		cnode->setContentSize(CCSizeMake(size.width() , size.height()));
	}
	else if ( property == Property("ScaleXY") )
	{
		QPointF point =variant.value<QPointF>();
		cnode->setScaleX(point.x());
		cnode->setScaleY(point.y());
	}
	else if ( property == Property("Rotation") )
	{
		double rotaion =variant.value<double>();
		cnode->setRotation(rotaion);
	}
	else if ( property == Property("Z-order") )
	{
		int zorder = variant.value<int>();
		if (cnode->getParent())
		{
			cnode->getParent()->reorderChild(cnode , zorder);
		}
	}
	else if ( property == Property("Tag") )
	{
		int tag = variant.value<int>();
		if (cnode->getParent())
		{
			cnode->setTag(tag);
		}
	}

    
    
}






